#include "Animation.h"
#include "Sprite.h"

/*-----------------------------------------------------------------------------------------------*/
Animation::Animation(AnimationInfo::AnimationInfoPtr animationInfo):
m_currentState(STOP),
m_pAnimationInfo(animationInfo),
m_currentFrame(0),
m_currentFrameTime(0)
{
}
/*-----------------------------------------------------------------------------------------------*/
void Animation::update(float deltaTime)
{
   assert( m_pAnimationInfo.get() );
   
   if ( m_currentState != PLAY )
   {
      return;
   }
 
   m_currentFrameTime += deltaTime;

   while( m_currentFrameTime > m_pAnimationInfo->getTimePerFrame() * m_pAnimationInfo->getSpeed() )
   {
      m_currentFrameTime -= m_pAnimationInfo->getTimePerFrame() * m_pAnimationInfo->getSpeed();
      m_currentFrame++;

      if ( m_currentFrame >= m_pAnimationInfo->getFrameCount() )
      {
         m_currentFrame = 0;
         if ( !m_pAnimationInfo->getLoopable() )
         {
            this->stop();
         }
      }
   }
}
/*-----------------------------------------------------------------------------------------------*/
void Animation::stop ()
{
   m_currentState = STOP;
}
/*-----------------------------------------------------------------------------------------------*/
void Animation::play ()
{
   m_currentState = PLAY;

   if(m_currentState == STOP)
   {
      m_currentFrame = 0;
      m_currentFrameTime = 0.0f;
   }
}